<?php

include('general_include.php');
include("class/json.php");

$email=trim($_POST['email']);
$password=trim($_POST['password']);

#if username password not empty, check the vald entry in table
if(!empty($email) && !empty($password))
{

	 $SqlLogin = "SELECT * FROM ".TABLEPREFIX."_user WHERE email=".mysql_quote($email)." AND password=".mysql_quote($password)." AND is_active='Y'"; 

	$RsLogin=$adodbcon->Execute($SqlLogin);		

	$NumLogin = $RsLogin->NumRows();

	if($NumLogin>0)
	{ 
		#Record found in database, set the session values
		$RowLogin=$RsLogin->FetchRow();		
       
		if(stripslashes($RowLogin["email"])==$email && (stripslashes($RowLogin["password"]))==$password)

		{	
			
			$_SESSION['user_id'] = $RowLogin['user_id'];
			$_SESSION['name'] = $RowLogin['name'];
			$_SESSION['last_log_in_date'] = $RowLogin['last_log_in_date'];
			$_SESSION['online_status'] = $RowLogin["online_status"];
			
			$updateUserStatusSql = "UPDATE ".TABLEPREFIX."_user SET is_logged_in = 1,  last_log_in_date = SYSDATE() WHERE user_id = '".$RowLogin['user_id']."'";
			$UserManagerObjAjax->Execute($updateUserStatusSql);
			/*$Sqlusertype = "SELECT * FROM ".TABLEPREFIX."_user_type_user_relation where user_id='".$RowLogin['user_id']."'";
			$typeArr=$UserManagerObjAjax->GetRecords("Row",$Sqlusertype);
			$user_type = $typeArr['user_type_id'];
			
			$_SESSION['user_type'] = $user_type;*/		
          

		    echo json_encode(

				array(

					'flag'	=>	1
				)
			);
		}
		else
		{
			#Record not found in database, Authentication failed
			echo json_encode(

				array(
					'flag'	=>	2
				)
			);
			//echo "Authentication failed";
		}
	}
	else
	{
			echo json_encode(

				array(

					'flag'	=>	3
				)
			);
		//echo "Authentication failed";
	}
}
else
{
				echo json_encode(

				array(

					'flag'	=>	4
				)
			);
	//echo "Authentication failed";
}
?>